package com.example.user.alarmreceiver;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Telephony;
import android.view.View;
import android.widget.Button;

import java.util.Calendar;

public class alarmService extends Activity implements View.OnClickListener {
    private static alarmService appRef = null;
    private Button b_call_service, b_exit_service;
    boolean k= false;

    public void onCreate(Bundle icicle){
        super.onCreate(icicle);
        appRef = this;
        setContentView(R.layout.main);
        b_call_service = (Button) findViewById(R.id.alarm);
        b_call_service.setOnClickListener(this);
        b_exit_service = (Button) findViewById(R.id.exit);
        b_exit_service.setOnClickListener(this);
    }

    public static alarmService getApp(){
        return appRef;
    }
    public void btEvent(String data){
        setTitle(data);
    }

    public void onClick(View arg0){
        if(arg0 == b_call_service){
            setTitle("Wait...Alarm=5");
            Intent intent = new Intent(alarmService.this,AlarmReceiver.class);
            PendingIntent p_intent = PendingIntent.getBroadcast(alarmService.this,0,intent,0);
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND,3);

            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),p_intent);
        }
        if(arg0 == b_exit_service){
            Intent intent = new Intent(alarmService.this,AlarmReceiver.class);
            PendingIntent p_intent = PendingIntent.getBroadcast(alarmService.this,0,intent,0);

            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
            alarmManager.cancel(p_intent);
            finish();
        }
    }
}
